from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.contrib.auth import authenticate
from django.views.decorators.http import require_http_methods
from .registerForm import RegisterForm
# 注册逻辑
# from django.contrib.auth import get_user_model
# User = get_user_model()
from .models import Patient
from django.contrib.auth.hashers import make_password


@require_http_methods(['GET', 'POST'])
def account_register(request):
    # phone = request.GET.get('account')
    # password = request.GET.get('password')
    form = RegisterForm(request.POST)
    print(request.POST.get('password'))
    if form.is_valid():
        phone = form.cleaned_data['phone']

        password = form.cleaned_data['password']

        # 使用 make_password 加密密码
        hashed_password = make_password(password)

        # 将加密后的密码存储到数据库
        Patient.objects.create(phone=phone, password=hashed_password)
        # Patient.objects.create(phone=phone, password=password)
        return HttpResponse(f"你好，注册成功,账号为：{phone}")
    else:
        return HttpResponse(f"注册失败")




def account_login(request):
    phone = request.GET.get('phone')
    password = request.GET.get('password')
    # 检查手机号是否存在
    try:
        patient = Patient.objects.get(phone=phone)  # 通过手机号获取患者记录
    except Patient.DoesNotExist:
        return "手机号不存在"

    return HttpResponse(f"你好，登录成功,账号为：{phone}")


def user_search(request, user_id):
    user_id = user_id
    return HttpResponse(f"你好，查询成功,账号为：{user_id}")